package com.atguigu.realtime.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Felix
 * @date 2023/10/13
 * 操作Redis的工具类
 */
public class RedisUtil {
    private static JedisPool jedisPool;

    static {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(100);
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setMaxWaitMillis(2000);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPool = new JedisPool(jedisPoolConfig,"hadoop105",6379,10000);
    }

    //获取操作Redis的客户端Jedis
    public static Jedis getRedisClient(){
        System.out.println("~~~获取Redis客户端~~~");
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }

    //关闭操作Redis的客户端Jedis
    public static void closeRedisClient(Jedis jedis){
        System.out.println("~~~关闭Redis客户端~~~");
        if(jedis != null ){
            jedis.close();
        }
    }

    //获取支持异步操作Redis的客户端连接
    public static StatefulRedisConnection<String,String> getAsyncRedisConnection(){
        System.out.println("~~~获取异步操作Redis的客户端~~~");
        //获取redis中的0号库，总共有16个库
        RedisClient redisClient = RedisClient.create("redis://hadoop105:6379/0");
        return redisClient.connect();
    }

    //关闭支持异步操作Redis的客户端连接
    public static void closeAysncRedisConnection(StatefulRedisConnection<String,String> redisConnection){
        if(redisConnection != null && redisConnection.isOpen()){
            System.out.println("~~~关闭异步操作Redis的客户端~~~");
            redisConnection.close();
        }
    }

    /**
     * 以异步的方式 从Redis中读取维度数据
     * @param redisConnection       异步客户端连接
     * @param redisKey              操作redis的key     维度表名:主键值
     * @return
     */
    public static JSONObject asyncGetDimInfo(StatefulRedisConnection<String,String> redisConnection,String redisKey){
        RedisAsyncCommands<String, String> asyncCommands = redisConnection.async();
        try {
            String dimStr = asyncCommands.get(redisKey).get();
            if(StringUtils.isNotEmpty(dimStr)){
                return JSON.parseObject(dimStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 以异步的方式  向Redis中缓存从hbase中查询到的维度
     * @param redisConnection       异步连接客户端
     * @param redisKey              操作redis的key
     * @param dimJsonObj            维度对象
     */
    public static void asyncWriteDim(StatefulRedisConnection<String,String> redisConnection,String redisKey,JSONObject dimJsonObj){
        RedisAsyncCommands<String, String> asyncCommands = redisConnection.async();
        asyncCommands.setex(redisKey,3600*24,dimJsonObj.toJSONString());
    }



    public static void main(String[] args) {
        Jedis jedis = getRedisClient();
        String pong = jedis.ping();
        System.out.println(pong);
    }
}
